#include "qmainview.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QListView>
#include <QDesktopWidget>
#include <QApplication>
#include <QGuiApplication>
#include <QScreen>
#include <QStandardItemModel>
#include <QStringListModel>
#include <QMessageBox>
#include <QModelIndex>
#include <QDebug>
#include <QDir>
#include <QHBoxLayout>
#include <QScrollArea>
#include <QScrollBar>
#include <QListWidget>
#include <QColor>

///程序管理器
#include "shellmanager.h"


void qmainview::initDataStructure(){
    smanager = new shellmanager;
}


qmainview::qmainview(QWidget *parent) : QWidget(parent)
{
    initDataStructure();
    qDebug() << "constructor";
    getDesktopSize();
    //maxmumSize();
    //showMaximized();
    //QVBoxLayout* vbox = new QVBoxLayout();
    QHBoxLayout* hbox = new QHBoxLayout();

    _lviewroot = new QListWidget();
    _lviewwork = new QListWidget();
    //_lviewroot->setSpacing(10);
    //_lviewroot->setBaseSize(50,100);


    initRootListView();
    initWorkListView();


//    QPushButton* pbtn = new QPushButton();
//    pbtn->setText("123456");
//    hbox->addWidget(pbtn);

    _lviewroot->setMinimumWidth(_width/2-20);
    _lviewwork->setMinimumWidth(_width/2-20);
    //_lviewroot->setMinimumHeight(_width/2-5);
    //_lviewwork->setMinimumHeight(_width/2-5);
    _lviewroot->verticalScrollBar()->setDisabled(true);
    _lviewwork->verticalScrollBar()->setDisabled(true);

    //QVBoxLayout* vboxroot= new QVBoxLayout;
    //vboxroot->addWidget(_lviewroot);
    hbox->addWidget(_lviewroot);

    //hbox->addLayout(vboxroot);
    hbox->addWidget(_lviewwork);
    //hbox->setContentsMargins(0,0,0,0);
    //hbox->addSpacing(6);
    //hbox->setMargin(5);

    ///下来条，防止文件过多展示不到桌面上
    QScrollArea* scroll = new QScrollArea;
    scroll->setContentsMargins(0,0,0,0);

    ///初始化一个widget用来存放Gridlayout
    QWidget *pWidget = new QWidget();
    pWidget->setLayout(hbox);

    int width = _lviewroot->width();
    int height = _lviewroot->height();
    qDebug() << "width:" << width << " height:" << height;

    ///设置样式，直接在函数中设置
//    _lviewroot->setStyleSheet("QListWidget{border:1px solid gray; color:black; }"
//                               "QListWidget::Item{padding-top:20px; padding-bottom:4px; }"
//                               "QListWidget::Item:hover{background:skyblue; }"
//                               "QListWidget::item:selected{background:lightgray; color:red; }"
//                               "QListWidget::item:selected:!active{border-width:0px; background:lightgreen; }"
//                               );

    pWidget->setMinimumHeight(22*(2+_workcols));

    ///将widget插入到scrolllayout中
    scroll->setWidget(pWidget);
    scroll->setWidgetResizable(true);

    ///初始化一个vboxlayout,将scrolllayout放进去
    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addWidget(scroll);
    mainLayout->setContentsMargins(0,0,0,0);

    //vbox->addLayout(hbox);
    //setLayout(vbox);
    setLayout(mainLayout);

    ///设置window样式
    setWindowState(Qt::WindowMaximized);
    ///无边框
    ///setWindowFlags(Qt::FramelessWindowHint);
    ///背景透明
    setAttribute(Qt::WA_TranslucentBackground);
}

///初始化仓库数据
void qmainview::initLeftView()
{

}

///初始化工作目录数据
void qmainview::initRightView()
{

}

void qmainview::initWorkListView(){
    _itemmodelwork = new QStandardItemModel(this);
    QStringList strList;
    ///遍历文件夹
    strList = reverseDir("D:\\WorkStation\\HolyCompare");

    int nCount = strList.size();
    for(int i = 0; i < nCount; i++)
    {
       QString string = static_cast<QString>(strList.at(i));
//       QStandardItem *item = new QStandardItem(string);
//       _itemmodelwork->appendRow(item);
        QListWidgetItem *dropbox_item = new QListWidgetItem();
        dropbox_item->setIcon(QIcon(":/res/png/stylesheet/open.jpg"));
        if(i%5 == 0){
            QColor color;
            color.setRed(100);
            //dropbox_item->setBackgroundColor(color);
            dropbox_item->setTextColor(color);
        }
        dropbox_item->setText(string);
        dropbox_item->setSizeHint(QSize(60, 22));
        _lviewwork->addItem(dropbox_item);
    }
    //_lviewwork->setModel(_itemmodelwork);
    //_lviewwork->setFixedSize(200,300);
    _workcols = nCount;

    connect(_lviewwork,SIGNAL(clicked(QModelIndex)),this,SLOT(listClick(QModelIndex)));
}

void qmainview::initRootListView(){
    _itemmodelroot = new QStandardItemModel(this);
    QStringList strList;
    ///遍历文件夹
    strList = reverseDir("D:\\WorkStation\\HolyCompare");

    int nCount = strList.size();
    for(int i = 0; i < nCount; i++)
    {
       QString string = static_cast<QString>(strList.at(i));
//       QStandardItem *item = new QStandardItem(string);
//       _itemmodelroot->appendRow(item);

        QListWidgetItem *dropbox_item = new QListWidgetItem();
        dropbox_item->setIcon(QIcon(":/res/png/stylesheet/open.jpg"));
        dropbox_item->setText(string);
        dropbox_item->setSizeHint(QSize(60, 22));
        _lviewroot->addItem(dropbox_item);
    }
    //_lviewroot->setModel(_itemmodelroot);
    //_lviewroot->setFixedSize(200,300);

    _rootcols = nCount;

    connect(_lviewroot,SIGNAL(clicked(QModelIndex)),this,SLOT(listClick(QModelIndex)));
}

QStringList qmainview::reverseDir(const QString & path){
    QStringList get_files;
    QDir dir(path);
    if(!dir.exists())
    {
        qDebug() << "it is not true dir_path";
    }
    QFileInfoList info_list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
    for(int i= 0 ; i< info_list.count(); i++)
    {
        QFileInfo info = info_list.at(i);
        if(info.isDir())
        {
            QString sub_dir  = info.absoluteFilePath();
            get_files.append(sub_dir);
            /*递归*/
            QStringList get_next_files = reverseDir(sub_dir);

            get_files.append(get_next_files);
        }else
        {
            QString file_name = info.absoluteFilePath();
            get_files.append(file_name);
        }
    }

    return get_files;
}

void qmainview::listClick(QModelIndex index){
    QString strTemp, strNextFile;
    strTemp = index.data().toString();
    strNextFile = index.siblingAtRow(1).data().toString();

    //QMessageBox msg;
    //msg.setText(strTemp);
    //msg.exec();

    QStringList arguments;
    arguments.append("/silent");
    arguments.append(strTemp);
    //QDir::currentPath();
    arguments.append(strNextFile);
    //dir E:\BeyondCompare\BCompare.exe   /fv="Folder Compare" "X:\AHK\框架试验\框架\gui框架4.0"  "X:\AHK\框架试验\框架\gui框架3.0"
    smanager->startProgram("D:\\Program Files\\Beyond Compare 4\\BCompare.exe", arguments);
}

void qmainview::maxmumSize()
{
    setMaximumWidth(_width);
    setMaximumHeight(_height);
}

void qmainview::getDesktopSize()
{
    ///已过时API
    ///获取桌面widget
    //QDesktopWidget* desktopWidget = QApplication::desktop();
    ///获取可用桌面大小
    //QRect deskRect = QApplication::desktop()->availableGeometry();
    ///获取主屏幕分辨率
    //QRect screenRect = QApplication::desktop()->screenGeometry();
    QScreen* screen = QGuiApplication::primaryScreen();
    _width = screen->geometry().width();
    _height = screen->geometry().height();
}
